import { cunDang, sheZhi } from './bianLiang.js'
import { ShuJuKu } from './ShuJuKu.js'
import { changJing } from '../changJing/ChangJing.js'
import { jieMian } from '../jieMian/zhuCeZuJian.js'
import { gengXinJiHe } from '../gengXin/gengXin.js'

class CunChu {
    shuJuKu = null

    constructor() {
        let sheZhiCunChu = window.localStorage.getItem('sheZhi')

        if (sheZhiCunChu) {
            sheZhiCunChu = JSON.parse(sheZhiCunChu)
            Object.assign(sheZhi, sheZhiCunChu)
        }

        this.shuJuKu = new ShuJuKu()
    }

    async jiaZai() {
        let daoRuCunDangAnNiu = jieMian.kaiShiCaiDan.daoRuCunDangAnNiu
        let { cunDangAnNiu, daoChuCunDangAnNiu, shanChuCunDangAnNiu } = jieMian.caiDan.sheZhiKuang.cunDangXiang

        daoRuCunDangAnNiu.onclick = () => this.daoRuCunDang()
        cunDangAnNiu.onclick = () => this.cunDang()
        daoChuCunDangAnNiu.onclick = () => this.daoChuCunDang()
        shanChuCunDangAnNiu.onclick = () => this.shanChuCunDang()

        gengXinJiHe.set(this, this.gengXin.bind(this))

        await this.duDang()
    }

    gengXin() {
        this.cunChuSheZhi()
    }

    cunDang() {
        changJing.cunDang()

        return this.shuJuKu.xiuGai(cunDang, 'cunDang')
    }

    daoRuCunDang() {
        let huiDiaoHanShu = shuJu => {
            try {
                Object.assign(cunDang, JSON.parse(shuJu))
                this.shuJuKu.xiuGai(cunDang, 'cunDang')
            } catch (error) {
                console.log(error);
            }
        }

        daoRuShuJu(huiDiaoHanShu)
    }

    duDang() {
        return this.shuJuKu.chaXun('cunDang').then(res => {
            Object.assign(cunDang, res)
        })
    }

    async daoChuCunDang() {
        await this.cunDang()

        return this.shuJuKu.chaXun('cunDang').then(res => {
            let cunDang = JSON.stringify(res, (k, v) => {
                // JSON默认不能正确处理缓存数组
                if (v instanceof Float32Array) return Array.from(v)
                return v
            })
            xiaZaiShuJu(cunDang)
        })
    }

    shanChuCunDang() {
        return this.shuJuKu.shanChu('cunDang').then(() => {
            document.location.reload()
        })
    }

    cunChuSheZhi() {
        let sheZhiCunChu = JSON.stringify(sheZhi)

        window.localStorage.setItem('kuaiJieJian', sheZhiCunChu)
    }
}

function daoRuShuJu(huiDiaoHanShu) {
    let daoRu = document.createElement('input')
    daoRu.type = 'file'
    daoRu.click()

    daoRu.onchange = shiJian => {
        let wenJian = shiJian.target.files[0]
        
        if (wenJian) {
            let reader = new FileReader();
            reader.onload = function (evt) {
                huiDiaoHanShu(evt.target.result)
            };
            reader.readAsText(wenJian);
        }

        // if (wenJian) wenJian.text().then(res => huiDiaoHanShu(res))
    }
}

function xiaZaiShuJu(canShu) {
    let wenJian = new Blob([canShu], { type: 'application/json' })
    let lianJie = URL.createObjectURL(wenJian)

    let xiaZai = document.createElement('a')
    xiaZai.download = 'cunDang'
    xiaZai.href = lianJie
    xiaZai.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, composed: true }))
}

export {
	CunChu
}